
	use "$root/output/analysis_finaldataset.dta", clear
		
	// Calculate over-estimate of children at expected learning level
	preserve
		drop if kn_learning==. | wb_learning==.
		gen overestimate=(kn_learning>wb_learning)
		fre overestimate
	restore
	
// Table 5: Beliefs about Reality
	local beliefs globalcrisis_binary nationalcrisis_binary kn_learning kn_eyos kn_spending_cont 			growth returns_estimate_male returns_estimate_female script_prior

	mat m = J(9,5,.)
	local y=1 // Initialize counter variable y to start at 1
	foreach var of varlist `beliefs' { 
		qui sum `var' , d
			mat m[`y',1] = round(r(mean),.01)
			mat m[`y',3] = round(r(sd),.01)
			mat m[`y',4] = round(r(N),.01)
		local y=`y'+1
	}

	local wbfacts wb_learning wb_schooling wb_spending 
	local returnfacts returns_actual_male returns_actual_female trueeffect

	preserve
		collapse `beliefs' `wbfacts' `returnfacts' , by(country_id)
		local y=3
		foreach var of varlist `wbfacts'{
		qui sum `var', d
				mat m[`y',2] = round(r(mean),.01)
				mat m[`y',5] = round(r(N),.01)
			local y=`y'+1
		}
		local y=7
		foreach var of varlist `returnfacts'{
		qui sum `var', d
				mat m[`y',2] = round(r(mean),.01)
				mat m[`y',5] = round(r(N),.01)
			local y=`y'+1
		}
	restore
	 
	mat colnames m = "Mean (Beliefs)" "Mean (Data)" "SE (Beliefs)" "N (Beliefs)"   "N (Data)"
	mat rownames m = "Global learning crisis (0/1)" "National learning crisis (0/1)" "10yr olds can read" "Average schooling (Years)" "Gov spend per child (USD)" ///
					 "Growth mindset (1-6)" "LM Returns for Boys" "LM Returns for Girls" "Effect of Reading Program (0-4)"

	estout matrix(m), varwidth(32)mlabel(none)
	esttab matrix(m) using tables/belief_descriptives.tex, replace booktabs mlabel(none)	

*--------------------------------
// Figure 1: Policymaker Beliefs and Data on Education Systems
*--------------------------------	

	local beliefs globalcrisis_binary nationalcrisis_binary kn_learning kn_eyos kn_spending_cont 				growth returns_estimate_male returns_estimate_female script_prior
				  
	local wbfacts wb_learning wb_schooling wb_spending 
	local returnfacts returns_actual_male returns_actual_female trueeffect

	//Generate reality variables	
	gen kn_learning_reality					= wb_learning 
	gen kn_eyos_reality 					= wb_schooling
	gen kn_spending_cont_reality			= wb_spending
	gen returns_estimate_male_reality		= returns_actual_male
	gen returns_estimate_female_reality		= returns_actual_female
	gen script_prior_reality				= trueeffect

	local beliefcompare kn_learning kn_eyos kn_spending_cont returns_estimate_male returns_estimate_female script_prior
	
	local facts kn_learning_reality kn_eyos_reality kn_spending_cont_reality returns_estimate_male_reality returns_estimate_female_reality script_prior_reality

	//Labels
	lab var kn_learning 			"Learning"
	lab var kn_eyos 				"Schooling"
	lab var kn_spending_cont 		"Spending"
	lab var returns_estimate_male 	"Returns (Male)"
	lab var returns_estimate_female "Returns (Female)"
	lab var script_prior 			"Scripting Intervention"

	drop if kn_spending_cont_reality>1000
	
	//Mini scatter
	preserve
		gen max_y=.
		gen max_x=.
		gen max=.
		foreach var of varlist `beliefcompare' {
			local l`var' : variable label `var'
		}
		collapse `beliefcompare' `facts' max_y max_x max , by(country)
		set graph off
		foreach var of varlist `beliefcompare'{
		qui sum `var'
		replace max_y=r(max)+1
		local max_y=max_y
		qui sum `var'_reality
		replace max_x=r(max)+1
		local max_x=r(max)+1	
		replace max=max_x
		replace max=max_y if max_y>max_x
		local max=max
		twoway (scatter `var' `var'_reality)(function y=x,range(0 `max')), ///
			   name(`var',replace)legend(off)xscale(range(0 `max_y'))yscale(range(0 `max_x'))title("`l`var''") ///
			   xlabel(#5)ylabel(#5)xtitle("Data")subtitle("Beliefs",pos(11)span)
		}
		
		graph combine `beliefcompare'
		graph export graphs/beliefcompare.pdf, replace
	restore

// Appendix	
	
*------------------------------------------------------
// Table with mean, sd and N with original sample only
*------------------------------------------------------
	
	//Keep only original sample
	keep if googlelist==1

	local beliefs globalcrisis_binary nationalcrisis_binary kn_learning kn_eyos kn_spending_cont 			growth returns_estimate_male returns_estimate_female script_prior

// Summary table - 2
	mat m = J(9,5,.)
	local y=1 // Initialize counter variable y to start at 1
	foreach var of varlist `beliefs' { 
		qui sum `var' , d
			mat m[`y',1] = round(r(mean),.01)
			mat m[`y',3] = round(r(sd),.01)
			mat m[`y',4] = round(r(N),.01)
		local y=`y'+1
	}

	local wbfacts wb_learning wb_schooling wb_spending 
	local returnfacts returns_actual_male returns_actual_female trueeffect

	preserve
		collapse `beliefs' `wbfacts' `returnfacts' , by(country)
		local y=3
		foreach var of varlist `wbfacts'{
		qui sum `var', d
				mat m[`y',2] = round(r(mean),.01)
				mat m[`y',5] = round(r(N),.01)
			local y=`y'+1
		}
		local y=7
		foreach var of varlist `returnfacts'{
		qui sum `var', d
				mat m[`y',2] = round(r(mean),.01)
				mat m[`y',5] = round(r(N),.01)
			local y=`y'+1
		}
	restore
	 
	mat colnames m = "Mean (Beliefs)" "Mean (Data)" "SE (Beliefs)" "N (Beliefs)"   "N (Data)"
	mat rownames m = "Global learning crisis (0/1)" "National learning crisis (0/1)" "10yr olds can read" "Average schooling (Years)" "Gov spend per child (USD)" ///
					 "Growth mindset (1-6)" "LM Returns for Boys" "LM Returns for Girls" "Effect of Reading Program (0-4)"

	estout matrix(m), varwidth(32)mlabel(none)

	esttab matrix(m) using tables/belief_descriptives_robustness.tex, replace booktabs mlabel(none)	
